<!--
 * @Author: your name
 * @Date: 2021-09-07 18:09:27
 * @LastEditTime: 2021-09-18 22:54:44
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \ej-webui\src\pages\role\customer.vue
-->
<template>
  <div class="production">
    <div class="content">
      <!-- 表格 -->
      <el-table :data="resData" style="width: 100%" size="small">
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column label="角色名称" prop="name"></el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button type="text" @click="editHandler(scope.row)">编辑</el-button>
            <el-button type="text" @click="privilegeHandler(scope.row)">授权</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 编辑模态框 -->
      <el-dialog
        :title="title" 
        :visible.sync="visible"
        @close="dialogCloseHandler">
        <el-form :model="production" :rules="rules" ref="saveForm">
          <el-form-item
            label="角色名称"
            :label-width="formLabelWidth"
            prop="name">
                <el-input v-model="production.name" clearable></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="visible = false">取消</el-button>
          <el-button type="primary" @click="toSavehandler">确定</el-button>
        </div>
      </el-dialog>
      <!-- 授权模态框 -->
      <el-dialog
        title="授权"
        :visible.sync="privilegeVisible"
        @close="dialogCloseHandler">
        <el-form :model="production" :rules="rules" ref="saveForm">
          <div class="role">
            <b>角色名称</b>  {{production.name}} 
          </div>
          <el-form-item
            label="权限"
            :label-width="formLabelWidth"
           >
          <!-- <el-cascader
              v-model="name"
              :options="categories"
          >
          </el-cascader> -->
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="visible = false">取消</el-button>
          <el-button type="primary" @click="toSavehandler">确定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import requset from "@/utils/request";
import qs from "qs";
import _ from "lodash";

export default {
  data() {
    return {
      resData: {},
      categories: [],
      title: "",
      visible: false,
      privilegeVisible:false,
      production:{},
      formLabelWidth: "80px",
       
      rules: {
        name: [
          { required: true, message: "产品名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
    
  methods: {
 
    // 查询所有角色
    async pageQueryProductions() {
      let res = await requset.get("/role/findAll",{});
      this.resData = res.data;
    },
    // 表单保存事件
    toSavehandler() {
      this.$refs["saveForm"].validate((valid) => {
        if (valid) {
          requset
            .post("/product/saveOrUpdate", qs.stringify(this.production))
            .then((res) => {
              this.pageQueryProductions(); // 重载数据
              this.$message.success(res.message); // 提示操作结果信息
              this.visible = false; // 关闭模态框
            });
        } else {
          return false;
        }
      });
    },
    // 模态框关闭的回调
    dialogCloseHandler() {
      this.$refs["saveForm"].resetFields(); // 置空表单验证
    },
    // 编辑按钮点击事件
    editHandler(row) {
      this.privilegeVisible=false;
      this.production = _.clone(row); // 将当前行数据赋值给数据模型
      this.title = "编辑角色信息";
      this.visible = true;
    },
    // 授权按钮点击事件
    privilegeHandler(row){
      this.visible=false;
      this.production=_.clone(row);
      this.privilegeVisible=true;
    }
  },
  created() {
    this.pageQueryProductions();  
  },
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.content>el-table>el-table-column:last-child{
    margin-left:800px;
}
</style>